﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.HVAC.LevelIndependentTag;
using YArchitech.Revit.RevitExtension;

namespace YArchitech.HVAC.DuctTerminalTag
{
	public class MarkDuctTerminalUtility
	{
		public static List<Line> DrawLineBox(Autodesk.Revit.DB.View view, XYZ startPoint, XYZ currentPoint, ref XYZ tagLocation, ref DirectionVector directionVector, double movingDistance, bool tagContentChecked, ref double verticalLineLength)
		{
			List<Line> list = new List<Line>();
			double num = AssistFunc.mmToFeet(MarkDuctTerminalUtility.fontHigtht * MarkDuctTerminalUtility.fontWidth * 160.0 * (double)MarkDuctTerminalUtility.tagBytes) * (double)view.Scale / 100.0;
			num += RevitVersionFuncs.GetLineFrontWidth(false);
			verticalLineLength = AssistFunc.mmToFeet(MarkDuctTerminalUtility.fontHigtht * 660.0 + 100.0 * MarkDuctTerminalUtility.leadDistance * 2.0) * (double)view.Scale / 100.0;
			XYZ startPoint2 = new XYZ();
			try
			{
				startPoint2 = MarkDuctTerminalUtility.GetTagBasePoint(view, startPoint, currentPoint, ref directionVector, movingDistance);
				XYZ xyz = currentPoint + directionVector.HorizontalVector * num;
				XYZ xyz2 = xyz + directionVector.HorizontalVector * num;
				XYZ xyz3 = currentPoint + directionVector.VerticalVector * verticalLineLength;
				XYZ endPoint = xyz3 + directionVector.HorizontalVector * num * 2.0;
				XYZ xyz4 = xyz3 + directionVector.VerticalVector * verticalLineLength;
				XYZ xyz5 = xyz4 + directionVector.HorizontalVector * num;
				XYZ endPoint2 = xyz5 + directionVector.HorizontalVector * num;
				XYZ endPoint3 = xyz + directionVector.VerticalVector * verticalLineLength;
				tagLocation = currentPoint;
				list.Add(YJKLineEx.YJKGetBound(startPoint2, currentPoint));
				list.Add(YJKLineEx.YJKGetBound(currentPoint, xyz4));
				list.Add(YJKLineEx.YJKGetBound(xyz, xyz5));
				if (tagContentChecked)
				{
					list.Add(YJKLineEx.YJKGetBound(currentPoint, xyz2));
					list.Add(YJKLineEx.YJKGetBound(xyz3, endPoint));
					list.Add(YJKLineEx.YJKGetBound(xyz4, endPoint2));
					list.Add(YJKLineEx.YJKGetBound(xyz2, endPoint2));
				}
				else
				{
					list.Add(YJKLineEx.YJKGetBound(currentPoint, xyz));
					list.Add(YJKLineEx.YJKGetBound(xyz3, endPoint3));
					list.Add(YJKLineEx.YJKGetBound(xyz4, xyz5));
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return list;
		}

		public static List<Line> DrawNoLineBox(Autodesk.Revit.DB.View view, XYZ startPoint, XYZ currentPoint, ref XYZ tagLocation, ref DirectionVector directionVector, double movingDistance)
		{
			List<Line> list = new List<Line>();
			double num = AssistFunc.mmToFeet(MarkDuctTerminalUtility.fontHigtht * MarkDuctTerminalUtility.fontWidth * 160.0 * (double)MarkDuctTerminalUtility.tagBytes) * (double)view.Scale / 100.0;
			XYZ startPoint2 = new XYZ();
			try
			{
				startPoint2 = MarkDuctTerminalUtility.GetTagBasePoint(view, startPoint, currentPoint, ref directionVector, movingDistance);
				XYZ endPoint = currentPoint + directionVector.HorizontalVector * num;
				tagLocation = currentPoint;
				list.Add(YJKLineEx.YJKGetBound(startPoint2, currentPoint));
				list.Add(YJKLineEx.YJKGetBound(currentPoint, endPoint));
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return list;
		}

		private static XYZ GetTagBasePoint(Autodesk.Revit.DB.View view, XYZ startPoint, XYZ currentPoint, ref DirectionVector directionVector, double dis)
		{
			XYZ result = new XYZ();
			if (startPoint.X <= currentPoint.X && startPoint.Y <= currentPoint.Y)
			{
				directionVector.HorizontalVector = view.RightDirection;
				directionVector.VerticalVector = view.UpDirection;
				result = startPoint + dis * view.UpDirection;
			}
			if (startPoint.X > currentPoint.X && startPoint.Y <= currentPoint.Y)
			{
				directionVector.HorizontalVector = -view.RightDirection;
				directionVector.VerticalVector = view.UpDirection;
				result = startPoint + dis * view.UpDirection;
			}
			if (startPoint.X > currentPoint.X && startPoint.Y > currentPoint.Y)
			{
				directionVector.HorizontalVector = -view.RightDirection;
				directionVector.VerticalVector = -view.UpDirection;
				result = startPoint - dis * view.UpDirection;
			}
			if (startPoint.X <= currentPoint.X && startPoint.Y > currentPoint.Y)
			{
				directionVector.HorizontalVector = view.RightDirection;
				directionVector.VerticalVector = -view.UpDirection;
				result = startPoint - dis * view.UpDirection;
			}
			return result;
		}

		public static double fontHigtht = 1.0;

		public static double fontWidth = 1.0;

		public static double leadDistance = 0.0;

		public static int tagBytes = 10;
	}
}
